const batPokerChatCounter= 'batPokerChatCounter';
const batPokerChat= 'batPokerChat'; 

$(document).ready(function() {
	dwr.engine.setActiveReverseAjax(true);
    $("#startGameButton").click(function() {
        if (!localStorage[batPokerChatCounter]) {
            localStorage[batPokerChatCounter] = 0;
         }
    	$('#startGameButton').hide();
    	$('.main').show();
    	//Engine.getStuff(handleGetData);
    	//$('#bot' + 1 + ' .card' + 1 ).fadeIn();
    	Engine.startGame();
    	//GameBean.getPot(handleGetData);
    	
    });
	
	$("#endGameButton").click(function () {
		if ( confirm('Oled Sa kindel, et soovid mängu lõpetada?')) {
			$('.main').fadeOut(700);
			Engine.endGame();
		}	
	});	
});

function handleGetData(str) {
	alert(str);
}

function humanAction(){
	$("#check").click(function () {
		$('.playerActionButton').fadeOut(250);
		Engine.humanCheck();	
	});
	$("#bet").click(function () {
		$('.playerActionButton').fadeOut(250);
		Engine.humanRaise();
	});
	$("#fold").click(function () {
		$('.playerActionButton').fadeOut(250);
		Engine.humanFold();	
	});
	$("#call").click(function () {
		$('.playerActionButton').fadeOut(250);
		Engine.humanCall();
	});
}

		// ** FUN ** //

function continueGame(){
	$("#continue").click(function () {
		$('.playerActionButton').fadeOut(250);
		Engine.nextRound();
	});
}


function giveCard( id, card ) {
	$('#bot' + id + ' .card' + card ).fadeIn(250);
}

function updatePlayerData ( id, name, action, betSize, money ) {
	$('#bot' +id+ ' .name').html( name );
	$('#bot' +id+ ' .action').html( action );
	if ( betSize != 0 ) {
		$('#bot' +id+ ' .betSize').html( 'Bet $' + betSize );
	} else {
		$('#bot' +id+ ' .betSize').html( '' );
	}	
	$('#bot' +id+ ' .amount').html( '$' + money );
}

function hidePlayer ( id ) {
	$('#bot' +id ).css({'visibility': 'hidden'});
}

function showPlayerButtons( canCheck, canCall, canBet ) {
	$('#fold').show();
	if ( canCheck ) $('#check').show();
	if ( canCall ) $('#call').show();
	if ( canBet ) $('#bet').show();
}

function showPlayerCards( id, i, cardNumber, cardSuit ) {
	$('#bot' + id + ' .card' + i + ' .top').html( cardNumber );
	$('#bot' + id + ' .card' + i + ' .bottom').html( cardNumber );
	$('#bot' + id + ' .card' + i + ' .smallSuit').html(''+ getSuit( cardSuit )+ '');
	$('#bot' + id + ' .card' + i ).addClass(''+ getCardColor( cardSuit )+'');
	$('#bot' + id + ' .card' + i ).removeClass('backSide');
}

function isDealer ( id ) {
	$('#bot' +id+ ' .playerInfo').css({'background-color': '#f3b336'});
}

function removeDealer ( id ) {
	$('#bot' +id+ ' .playerInfo').css({'background-color': '#D3D3D3'});
}

function updatePotAmount ( amount ) {
	$('#pot').html( 'Pot: $'+amount );
}

function foldPlayerCards( id ) {
	$('#bot' + id + ' .card1').fadeOut(700);
	$('#bot' + id + ' .card2').fadeOut(700);
}

function isActivePlayer( id ) {
	$('#bot' +id).addClass('activePlayer');
}

function removeActivePlayer( id ) {
	$('#bot' +id).removeClass('activePlayer');
}

function writeCommunityCardToHtml( cardPos, suit, number ) {
	switch ( suit ) {
	case 'S':
		$('#community' + cardPos + '').addClass('black');
		$('#community' + cardPos + ' .suit').html('&spades;');
		break;
	case 'H':
		$('#community' + cardPos + '').addClass('red');
		$('#community' + cardPos + ' .suit').html('&hearts;');
		break;
	case 'D':
		$('#community' + cardPos + '').addClass('red');
		$('#community' + cardPos + ' .suit').html('&diams;');
		break;
	case 'C':
		$('#community' + cardPos + '').addClass('black');
		$('#community' + cardPos + ' .suit' ).html('&clubs;');
		break;	
	}
	
	$('#community' + cardPos + ' .top').html( number );
	$('#community' + cardPos + ' .bottom' ).html( number );
	$('#community' + cardPos + '').fadeIn(700);
}

function clearTable() {
	$('#community0').fadeOut(700).removeClass('red black');
	$('#community1').fadeOut(700).removeClass('red black');
	$('#community2').fadeOut(700).removeClass('red black');
	$('#community3').fadeOut(700).removeClass('red black');
	$('#community4').fadeOut(700).removeClass('red black');
	updatePotAmount( 0 );
}

function clearPlayerHand( id ){
	for( var j = 1; j < 3; j++){
		$('#bot' + id + ' .card' +j+ ' .top').empty();
		$('#bot' + id + ' .card' +j+ ' .bottom').empty();
		$('#bot' + id + ' .card' +j+ ' .smallSuit').empty();
		$('#bot' + id + ' .card' + j).removeClass('red black');
		$('#bot' + id + ' .card' + j).addClass('backSide');
		$('#bot' + id + ' .card' + j).addClass('backSide');
	}
}

function clearPlayerData( id ){
	$('#bot' + id + ' .action').html( '' );
	$('#bot' + id + ' .betSize').html( '' );
	
}

function resetActivePlayerMarker( id ){
	$('#bot' + game.players[i].id).removeClass('activePlayer');
}

/*function resetHtmlForStage(){
	resetActivePlayerMarker();
	clearPlayerData();

function resetHtmlForRound(){
	clearPlayerHands();
	clearTable();
	resetHtmlForStage();
}
*/

function getSuit( suit ) {
	switch ( suit ) {
	case 'S':
		return '&spades;';
		break;
	case 'H':
		return '&hearts;';
		break;
	case 'D':
		return '&diams;';
		break;
	case 'C':
		return '&clubs;';
		break;	
	}
}

function getCardColor( suit ) {
	switch ( suit ) {
	case 'S':
		return 'black';
		break;
	case 'H':
		return 'red';
		break;
	case 'D':
		return 'red';
		break;
	case 'C':
		return 'black';
		break;	
	}
}

function logger( text ) {
	if ( viewLog == true ) {
		var show = $('#logArea').html() + text + '<br/>'; 
		$('#logArea').html(show);
		$('#logArea').animate({ scrollTop: 60000 }, 'slow');
		return false;
	} 
} 

//CHAT

function getPreviousMessages()
{
    ChatRoomDatabase.getChatContent({
    callback:function(messages) 
    {
      var chatArea=dwr.util.byId('chatArea');
      var html="";
      for(index in messages)
      {
         var msg=messages[index];
         html+=msg;
      }
      chatArea.innerHTML=html;
      var chatAreaHeight = chatArea.scrollHeight;
      chatArea.scrollTop = chatAreaHeight;
    }
    });

}

function newMessage(message)
{
  var chatArea=dwr.util.byId('chatArea');
  var oldMessages=chatArea.innerHTML;
  chatArea.innerHTML=oldMessages+message;
  addMessage(message);
  var chatAreaHeight = chatArea.scrollHeight;
  chatArea.scrollTop = chatAreaHeight;
}

function sendMessageIfEnter(event) {
	if(event.keyCode == 13) {
		sendMessage();
		Chat.botChatToHTML();
	}
}

function sendMessage()
{
    var message=dwr.util.byId('messageText');
    var messageText=message.value;
    Chat.postMessage(messageText, "Batman");
    message.value='';
}

function addMessage( message ) {
    var i = localStorage[batPokerChatCounter];
    localStorage[batPokerChat+ i] = message;        
    i++;
    localStorage[batPokerChatCounter] = i;
}

//LOGGER
function getPreviousLog()
{
    ChatRoomDatabase.getChatContent({
    callback:function(messages) 
    {
      var logArea=dwr.util.byId('logArea');
      var html="";
      for(index in messages)
      {
         var msg=messages[index];
         html+=msg;
      }
      logArea.innerHTML=html;
      var chatAreaHeight = logArea.scrollHeight;
      logArea.scrollTop = logAreaHeight;
    }
    });

}

function newLog(message)
{
  var logArea=dwr.util.byId('logArea');
  var oldMessages=logArea.innerHTML;
  logArea.innerHTML=oldMessages+message;  
  var logAreaHeight = logArea.scrollHeight;
  logArea.scrollTop = logAreaHeight;
}

function sendLog(logtext)
{
    Logger.postLOG(logtext);
    message.value='';
}